﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Graphics;

using FXnaEngine.Graphics;

using System.IO;

using Microsoft.Xna.Framework.Storage;



namespace FXnaEngine.Components
{
    public class FTerrainComponent:FRendererComponent
    {





        public FTerrainComponent(FEntity parent):base(parent)
            
       {


 



        }


        FTerrain terrain;

        public FTerrain Terrain
        {
            get { return terrain; }
            set {


                if (value != null)
                {

                    terrain = value;

                    terrain.BuildMesh();


                    //创建包围圆圈,这里只是一个测试方法

                   

                    float radius = Math.Max(terrain.WidthGeoLength,terrain.HeightGeoLength) / 2;


                    float y = (terrain.MaxHeight - terrain.MinHeight) / 2;


                    this.BoundingSphere = new BoundingSphere(new Vector3(terrain.WidthGeoLength/2,y, terrain.HeightGeoLength/2), radius);



                    this.BoundingSphere = this.BoundingSphere.Transform(this.parentEntity.Transform.WorldMatrix);



                }
                else
                {

                    this.BoundingSphere = new BoundingSphere();
                }

               


            
            
            }
        }







       


        public override bool ExecuteMessage(IMessage message)
        {


            return true;



        }



        protected override void RenderCore(FGraphicsSystem graphics)
        {
            base.RenderCore(graphics);



            if (this.terrain!=null)
            {

                graphics.ShaderBasic.SetParameters(this.material, this.parentEntity.Transform.WorldMatrix);


                graphics.RenderWithEffect(this.terrain.InnerMesh, graphics.ShaderBasic.BasicEffect);
                
            }





         







        }


    }
}
